package com.szzg10.thinking.chapter18;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class UsingBuffers {

	public static void main(String[] args) {
		char[] data = "UsingBuffers".toCharArray();
		
		ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 2);
		
		CharBuffer charBuffer = byteBuffer.asCharBuffer();
		charBuffer.put(data);
		charBuffer.flip();
		System.out.println(charBuffer.toString());
		
		charBuffer.rewind();
		symmetricScramble(charBuffer);
		charBuffer.flip();
		System.out.println(charBuffer.toString());
		
		charBuffer.rewind();
		symmetricScramble(charBuffer);
		charBuffer.flip();
		System.out.println(charBuffer.toString());
	}
	
	private static void symmetricScramble(CharBuffer charBuffer) {
		while(charBuffer.hasRemaining()) {
			charBuffer.mark();
			char a = charBuffer.get();
			char b = charBuffer.get();
			charBuffer.reset();
			charBuffer.put(b).put(a);
		}
	}
}
